package dennis;

import java.util.*;

public class BasicFeaturesV1 {
  
	public static final int SIZE = 999;
	ArrayList<Task> everything;
	ArrayList<Task> viewList; // to store tasks that are viewed
	ArrayList<Task> doneList;
  
	public BasicFeaturesV1(){
		everything = new ArrayList<Task>(SIZE); 
		viewList = new ArrayList<Task>(SIZE);
		doneList = new ArrayList<Task>(SIZE);
    }
  
	public void addTask (Task task) { //pass the whole object
		everything.add(task);  
		sort(); // call sort method to sort the Task objects according to upcoming
	}
  
	public void removeTask(int index) { 
		everything.remove(viewList.get(index-1));
		viewList.remove(index-1);
	}
	
	public void copyTask(int index) { 
		doneList.add(viewList.get(index-1));
	}
  
	public ArrayList<Task> viewbyDate (int index) { // viewlist by date
		viewList.clear();
		Task taskinlist;
		int year = index%100;
		int month = (index/100)%100;
		int date = ((index/100)/100)%100;
		int size = everything.size();
    
		for (int i=0; i<size; i++) {
			taskinlist = everything.get(i);
			if (taskinlist.getYear() == year && taskinlist.getMonth() == month && taskinlist.getDate() == date)
				viewList.add(taskinlist);
		}
    
		return viewList;
	}
  
	public ArrayList<Task> viewbyDatetoDate (int startDate, int endDate) { // viewlist by start date to end date
		viewList.clear();
		Task taskinlist;
		int year1 = startDate%100;
		int month1 = (startDate/100)%100;
		int date1 = (startDate/10000)%100;
		int year2 = endDate%100;
    	int month2 = (endDate/100)%100;
    	int date2 = (endDate/10000)%100;
    	int size = everything.size();
    
    	Calendar startdate = Calendar.getInstance();
    	Calendar enddate = Calendar.getInstance();
    	startdate.set(year1, month1, date1);
    	enddate.set(year2, month2, date2);
    	Calendar taskdate = Calendar.getInstance();
    	
    	for (int i=0; i<size; i++) {
    		taskinlist = everything.get(i);
    		taskdate.set(taskinlist.getYear(), taskinlist.getMonth(), taskinlist.getDate());
    		if(taskdate.after(startdate) && taskdate.before(enddate) || taskdate.equals(startdate) || taskdate.equals(enddate))
    			viewList.add(taskinlist);
    	}
    	
    	return viewList;
    }
  
	 public ArrayList<Task> viewbyLabel (String label) { // viewlist by label, String para in the form :label
		 viewList.clear();
		 String temp= ":";
		 for (int i=0; i<everything.size(); i++) {
			 temp = temp + everything.get(i).label;
			 if (temp.equals(label)){
				 viewList.add(everything.get(i)); 
			 }
			 temp= ":";
		 }
		 return viewList;
	 }
  
	 public ArrayList<Task> viewbyImportance() { // 1 = important
		 viewList.clear();
    
		 for (int i=0; i<everything.size(); i++) {
			 if (everything.get(i).hasImportance){
				 viewList.add(everything.get(i));
			 }
		 }
		 return viewList;
	 }
	 
	 public ArrayList<Task> viewDone() {
		 viewList = doneList;
		 return viewList;
	 }

	 public void sort() {
		 int a, i, j=0;
		 int size = everything.size();
		 Task temp1, temp2;
    
		 for (a=0; a<size; a++) {
			 for (i=1; i<size; i++) {
				 j = i - 1;
				 temp1 = everything.get(j);
				 temp2 = everything.get(i);
        
				 Calendar date1 = Calendar.getInstance();
				 Calendar date2 = Calendar.getInstance();
        
				 date1.set(temp1.getYear(), temp1.getMonth(), temp1.getDate(), temp1.getStartTimehours(), temp1.getStartTimeminutes());
				 date2.set(temp2.getYear(), temp2.getMonth(), temp2.getDate(), temp2.getStartTimehours(), temp2.getStartTimeminutes());
        
				 if(date1.after(date2)){
					 everything.set(i, temp1);
					 everything.set(j, temp2);
				 }	
			 }
		 } 
	}

	public ArrayList<Task> retrieveUpcomingTask() { // viewall
		viewList.clear();
		Task taskinlist;
		int size = everything.size();
    
		System.out.println("From BasicFeatures: retrieveUpcomingTask() in ViewList");
		for (int i=0; i<size; i++) {
			taskinlist = everything.get(i);
			//System.out.println(taskinlist.printTask());
			viewList.add(taskinlist);
		}
		return viewList;
	}
  
	public ArrayList<Task> retrieveUpcomingTask(int index) { // index is the number of next upcoming tasks
		viewList.clear();
		Task taskinlist;
		int i=0;
    
		if (everything.size()<index) {
			for (i=0; i<everything.size(); i++) {
				taskinlist = everything.get(i);
				viewList.add(taskinlist);
			}
		}
    
		else {
			for (i=0; i<index; i++) {
				taskinlist = everything.get(i);
				viewList.add(taskinlist);
			}
		}
		
		return viewList;
	}
  
	public Task specificTask(int index) {
		viewList.clear();
		Task taskinlist = new Task();
    
		for(int i=0; i<everything.size(); i++) {
			if (i==index-1)
				taskinlist = everything.get(i);
		}
		return taskinlist;
	}

	public ArrayList<Task> searchbyKeyword(String keyword) {
		viewList.clear();
	    Task taskinlist = new Task();
	    
	    for (int i=0; i<everything.size(); i++) {
	    	taskinlist = everything.get(i);
	      
	    	String date = Integer.toString(taskinlist.getDate());
	    	String month = Integer.toString(taskinlist.getMonth());
	    	String year = Integer.toString(taskinlist.getYear());
	    	String starttime = Integer.toString(taskinlist.getStartTime());
	    	String endtime = Integer.toString(taskinlist.getEndTime());
	    
	    	if ((date.indexOf(keyword)!=-1) || (month.indexOf(keyword)!=-1) || (year.indexOf(keyword)!=-1) || (starttime.indexOf(keyword)!=-1) || (endtime.indexOf(keyword)!=-1) || (taskinlist.getDescription().indexOf(keyword)!=-1) || (taskinlist.getLabel().indexOf(keyword)!=-1) || (taskinlist.getVenue().indexOf(keyword)!=-1)) {
	    		viewList.add(taskinlist);
	    		printTask(taskinlist);
	    	}
	    }
	    return viewList;
	}
  
	public void modify(int index, Task newtask) {
		Task oldtask = viewList.get(index-1);
	  
		for (int i=0; i<everything.size(); i++) {
			if (everything.get(i).compareTask(oldtask)) {
				everything.set(i, newtask);
				break;
			}
		}
		
		viewList.set(index-1, newtask);
	}

	public void printTask(Task task){
		System.out.print("Print Task only\n");
		System.out.print(task.getDate());System.out.print(" ");
		System.out.print(task.getMonth());System.out.print(" ");
		System.out.print(task.getYear());System.out.print(" ");
		System.out.print(task.getStartTime());System.out.print(" ");
		System.out.print(task.getEndTime());System.out.print(" ");
		System.out.print(task.getDescription());System.out.print(" ");
		System.out.print(task.getLabel());System.out.print(" ");
		System.out.print(task.getVenue());System.out.print(" ");
	}
  
	public void printeverything() {
		int size = everything.size();

		System.out.println("From BasicFeatures: printEverything in MASTER list()");
		
		for (int i=0; i<size; i++) {
			System.out.print(everything.get(i).getDate());
			System.out.print(" ");
			System.out.print(everything.get(i).getMonth());
			System.out.print(" ");
			System.out.print(everything.get(i).getYear());
			System.out.print(" ");
			System.out.print(everything.get(i).getstrStartTime());
			System.out.print(" ");
			System.out.print(everything.get(i).getstrEndTime());
			System.out.print(" ");
			System.out.print(everything.get(i).getDescription());
			System.out.print(" ");
			System.out.print(everything.get(i).getLabel());
			System.out.print(" ");
			System.out.print(everything.get(i).getVenue());
			System.out.print(" ");
			System.out.print("\n");
		}
	}
	
	public void printviewlist() { //testing purpose
		int size = viewList.size();

		System.out.println("From BasicFeatures: printviewlist()");
		
		for (int i=0; i<size; i++) {
			System.out.print(viewList.get(i).getDate());
			System.out.print(" ");
			System.out.print(viewList.get(i).getMonth());
			System.out.print(" ");
			System.out.print(viewList.get(i).getYear());
			System.out.print(" ");
			System.out.print(viewList.get(i).getstrStartTime());
			System.out.print(" ");
			System.out.print(viewList.get(i).getstrEndTime());
			System.out.print(" ");
			System.out.print(viewList.get(i).getDescription());
			System.out.print(" ");
			System.out.print(viewList.get(i).getLabel());
			System.out.print(" ");
			System.out.print(viewList.get(i).getVenue());
			System.out.print(" ");
			System.out.print("\n");
		}
	}
}
